package com.hrm.controller;


import com.hrm.entity.User;
import com.hrm.module.Dto;
import com.hrm.utils.Constants;
import com.hrm.utils.DtoUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;


/**
 * 用户管理
 *
 * @author 李雪阳
 * @version 1.0
 * @date 2020/8/1 17:00
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private com.hrm.service.xfl.UserService userService;
    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "loginForm";
    }

    /**
     * 查询所有userList
     * @param map
     * @return
     */
    @RequestMapping("/list")
    public String UserList(@RequestParam(value = "content",required = false) String content,ModelMap map){
        User user=new User();
        user.setLoginname(content);
        List<User> userList = userService.selectUserList(user);
        System.out.println(userList.size());
        for (User user1:userList) {
            System.out.println(user1.getCreateDate());
        }
        map.addAttribute("list",userList);
        return "user/list";
    }

    /**
     * 根据ID查询个人信息
     * @param session
     * @return
     */
    @GetMapping("/user")
    public ModelAndView  user(HttpSession session){
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        User user1 = userService.selectUser(user.getId());
        ModelAndView view = new ModelAndView();
        view.setViewName("user/my");
        view.addObject("user",user1);
        return view;
    }

    /**
     * 跳转修改页面
     * @param session
     * @return
     */
    @GetMapping("/skip")
    public ModelAndView uservoiw(HttpSession session){
//        User user = (User) session.getAttribute(Constants.USER_SESSION);
        User user1 = userService.selectUser(1);
        ModelAndView view = new ModelAndView();
        view.setViewName("user/myupdate");
        view.addObject("user",user1);
        return view;
    }


    /**
     * 修改个人信息
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/updateuser")
    public ModelAndView  updateuser(User user,HttpSession session){
        int i = userService.updateUser(user);
        ModelAndView view = new ModelAndView();
        if (i>0){
            view.setViewName("user/my");
        }
        return view;
    }


    /**
     * 根据id删除user
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public Dto deleteUser(@RequestParam("id") Integer id){
        Dto<T> dto=null;
        int i = userService.deleteUserInfo(id);
        if (i>0){
            dto = DtoUtil.returnSuccess("删除成功");
        }else {
            dto = DtoUtil.returnSuccess("删除失败");
        }
        return dto;
    }

    /**
     * 根据id查询user
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/update")
    public String update(@RequestParam("id") Integer id, ModelMap map){
        User user = userService.selectUserById(id);
        if (user!=null){
            map.addAttribute("user",user);
        }
        return "user/myupdate";
    }

    /**
     * 修改user
     * @param user
     * @return
     */
    @PostMapping("/myupdate")
    public String updateUserInfo(User user, HttpSession session){
        User user_session = (User) session.getAttribute("user_session");
        user.setCuid(user_session.getId());
        int i=userService.updateUserInfo(user);
        if (i>0){
            return "user/list";
        }
        return null;
    }

    /**
     * 跳转添加界面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "user/add";
    }

    /**
     * 添加user
     * @param user
     * @param map
     * @return
     */
    @PostMapping("/addUser")
    public String addUserInfo(User user,ModelMap map,HttpSession session){
        User user_session = (User) session.getAttribute("user_session");
        user.setCuid(user_session.getId());
        int i=userService.addUserInfo(user);
        System.out.println(i);
        return "user/list";
    }
}
